package cn.itcast.bos.web.action.base;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.data.domain.Page;

import com.mchange.v2.codegen.bean.PropertyMapConstructorGeneratorExtension;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Area;

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
	
	protected T model;
	
	@Override
	public T getModel() {
		return model;
	}
	
	public BaseAction(){
		//构造子类Action,获取继承父类型的范型
		
		Type genericSuperclass = this.getClass().getGenericSuperclass();
		//获取第一个范型参数
		ParameterizedType parameterizedType= (ParameterizedType) genericSuperclass;
		Class<T> modelClass= (Class<T>) parameterizedType.getActualTypeArguments()[0];
		try {
			model=modelClass.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	
	protected  int page;
	protected int rows;

	public void setPage(int page) {
		this.page = page;
	}


	public void setRows(int rows) {
		this.rows = rows;
	}
	protected void pushPageDataToValuestack(Page<T> pageData) {
		Map<String, Object> result= new HashMap<String,Object>();
		result.put("total", pageData.getTotalElements());
		result.put("rows", pageData.getContent());
		
		ActionContext.getContext().getValueStack().push(result);
	}

}
